package de.bazinga.rotw.treasure;

import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.message.type.CertifiedMessage;
import com.captiveimagination.jgn.message.type.PlayerMessage;
import com.captiveimagination.jgn.message.type.TimestampedMessage;

import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.gameModes.TreasureHunt;
import de.bazinga.rotw.network.Server;
import de.bazinga.rotw.network.message.GameMessage;
import de.bazinga.rotw.person.OpponentList;

public class TreasureFoundMessage extends Message implements CertifiedMessage, PlayerMessage, TimestampedMessage, GameMessage {
	
	private int treasureIndex = -1;

	public void setTreasureIndex(int treasureIndex) {
		this.treasureIndex = treasureIndex;
	}

	public int getTreasureIndex() {
		return treasureIndex;
	}
	
	public void handleMessage(RotwGameState game, Message msg, boolean server) {
		if(!(msg instanceof TreasureFoundMessage)) return ;

		TreasureFoundMessage message = (TreasureFoundMessage)msg;
		
		if(server) {
			((TreasureHunt)game.getGameMode()).getTreasureList().get(message.getTreasureIndex()).found(
					OpponentList.getInstance().getOpponent(getPlayerId()),true);
			((Server)game.getSyncManager()).getServer().sendToAllExcept(message, getPlayerId());
		}else{
			((TreasureHunt)game.getGameMode()).getTreasureList().get(message.getTreasureIndex()).found(
					OpponentList.getInstance().getOpponent(getPlayerId()),false);
		}	
	}
	
}